﻿
using MCPlatform.Communicate.TcpIp;
using System;
using System.Windows;

namespace MCPlatform.UI.Controls.Views.Communicate
{
    /// <summary>
    /// ClientDebug.xaml 的交互逻辑
    /// </summary>
    public partial class TcpClientDebugView : Window
    {
        NTcpClient nTcpClient;

        public string ClientName => nTcpClient?.TcpClientInfo.ClientName ?? "null";
        public TcpClientDebugView(NTcpClient tcpClient)
        {
            InitializeComponent();
            this.DataContext = this;
            this.nTcpClient = tcpClient;
            nTcpClient.DataReceived += NTcpClient_DataReceived;
        }

        private void NTcpClient_DataReceived(object? sender, Message e)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {

                rtb_recive.AppendText(e.MessageString);
               // rtb_recive.AppendText(Environment.NewLine);

            });
            
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            rtb_recive.Document.Blocks.Clear();
        }

        private void sendButton_Click(object sender, RoutedEventArgs e)
        {
            if (tb_sendData.Text==string.Empty)
            {
                MessageBox.Show("请输入发送的数据");
            }
            if (!nTcpClient.Connected)
            {
                MessageBox.Show($"{ClientName}连接已断开");
                this.Close();
                return;
            }
            nTcpClient.Write(tb_sendData.Text);
        }


    }
}
